#!/bin/bash
###
# @Author: Logan.Li
# @Gitee: https://gitee.com/attacker
# @email: admin@attacker.club
# @Date: 2025-03-14 06:07:30
# @LastEditTime: 2025-03-14 06:07:35
# @Description: 服务监控系统安装脚本
###

# 基础目录
BASE_DIR="/opt/monitor"
SCRIPT_DIR="$BASE_DIR/scripts"
CONFIG_DIR="$BASE_DIR/config"
LOG_DIR="$BASE_DIR/logs"

# 创建必要的目录
echo "创建目录结构..."
sudo mkdir -p "$SCRIPT_DIR" "$CONFIG_DIR" "$LOG_DIR"

# 复制脚本和配置文件
echo "复制文件..."
sudo cp "$(dirname "$0")/scripts/check_service.sh" "$SCRIPT_DIR/"
sudo cp "$(dirname "$0")/config/services.ini" "$CONFIG_DIR/"

# 设置权限
echo "设置权限..."
sudo chmod +x "$SCRIPT_DIR/check_service.sh"
sudo chmod 644 "$CONFIG_DIR/services.ini"

# 创建日志文件
sudo touch "$LOG_DIR/service_monitor.log"
sudo chmod 644 "$LOG_DIR/service_monitor.log"

# 添加cron任务
echo "配置定时任务..."
(crontab -l 2>/dev/null | grep -v "$SCRIPT_DIR/check_service.sh"; echo "* * * * * $SCRIPT_DIR/check_service.sh") | crontab -

echo "安装完成！"
echo "服务监控已安装在 $BASE_DIR"
echo "配置文件：$CONFIG_DIR/services.ini"
echo "日志文件：$LOG_DIR/service_monitor.log"
echo "可以通过编辑 $CONFIG_DIR/services.ini 来添加或修改监控的服务"
